#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 1994  Ling Li
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import gobject, gtk, os
from gtk import *

import config, filetype
from coralftp_globals import *
from configuration import CONFIG_FILE, CONFIG_PREFIX
from utils import *

class SystemEnvDialog(gobject.GObject):

    __gsignals__ = {
        'response' :
        (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT,)),
        }

    need_restart = False

    def __init__(self, coralftp):
        self.__gobject_init__()
        self.__coralftp = coralftp
        self.__xml = get_glade_xml('system_env_dialog')
        self.__xml.signal_autoconnect(self)
        self.__widget = self.__xml.get_widget('system_env_dialog')

        methods = config.get_available_methods()
        widget = self.__xml.get_widget('radio_config_standard')
        widget.set_property('sensitive', 'standard' in methods)
        if self.__coralftp.config.name == 'standard':
            widget.set_property('active', True)
        widget = self.__xml.get_widget('radio_config_gconf')
        widget.set_property('sensitive', 'gconf' in methods)
        if self.__coralftp.config.name == 'gconf':
            widget.set_property('active', True)

        method = coralftp.config.get_string('system', 'filetype_detection')
        methods = filetype.get_available_methods()
        widget = self.__xml.get_widget('radio_filetype_standard')
        widget.set_property('sensitive', 'standard' in methods)
        if method == 'standard':
            widget.set_property('active', True)
        widget = self.__xml.get_widget('radio_filetype_vfs')
        widget.set_property('sensitive', 'gnome_vfs' in methods)
        if method == 'gnome_vfs':
            widget.set_property('active', True)
        return

    def destroy(self):
        self.__widget.destroy()
        return

    def run(self):
        self.__widget.show()
        self.__widget.set_modal(True)
        return

    def on_response(self, obj, resp, *args):
        if resp == RESPONSE_OK:
            widget = self.__xml.get_widget('radio_filetype_standard')
            if widget.get_property('active'):
                method = 'standard'
            else:
                method = 'gnome_vfs'
            old = self.__coralftp.config.get_string(
                'system', 'filetype_detection')
            if old != method:
                self.__coralftp.config.set_string(
                    'system', 'filetype_detection', method)
                filetype.set_default(method)
            widget = self.__xml.get_widget('radio_config_standard')
            if widget.get_property('active'):
                method = 'standard'
            else:
                method = 'gconf'
            if self.__coralftp.config.name != method:
                if method == 'standard':
                    old = self.__coralftp.config
                    self.__coralftp.config = config.create_standard_from_gconf(
                        os.path.join(coralftp_schemadir, 'coralftp.schemas'),
                        CONFIG_FILE, self.__coralftp.config)
                    old.delete()
                else:
                    old = self.__coralftp.config
                    self.__coralftp.config = config.create_gconf_from_standard(
                        os.path.join(coralftp_schemadir, 'coralftp.schemas'),
                        CONFIG_PREFIX, self.__coralftp.config)
                    old.delete()
                self.need_restart = True
        self.emit('response', resp, *args)
        return

gobject.type_register(SystemEnvDialog)
